package com.jewelry.common.config;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.system.ServerContext;

/**
 * 系统配置管理
 * @author yangfeng
 *
 */
public class ConfigManager {

	private static Logger logger = LoggerFactory.getLogger(ConfigManager.class);

	private static XMLConfiguration config = null;
	

	/**
	 * 加载系统配置
	 */
	public static void load(String systemConfigFile) {
		try {
			config = new XMLConfiguration(systemConfigFile);
			config.setReloadingStrategy(new FileChangedReloadingStrategy());
		} catch (ConfigurationException e) {
			logger.error("Load ConfigFile Error! configFile {}", systemConfigFile, e);
			System.out.println("Load ConfigFile Error! configFile =" + systemConfigFile);
			System.exit(-1);
		}
	}

	// 为Spring提供配置文件加载
	public static String getPropertyFile(String fileName) {
		return "file:" + ServerContext.getSystemBaseConfigPath() + File.separator + fileName;
	}

	/**
	 * 获取配置对象,不对上层开放
	 * @return
	 */
	public static Configuration getConfig() {
		return config;
	}

	public static String getConfigString(String key) {
		return config.getString(key);
	}
	
	/**
	 * 读取一组值
	 * 注意:不能增加desc属性
	 * @param prefix
	 * @return
	 */
	public static Map<String,String> getKeys(String prefix){
		Iterator<String> iterator = config.getKeys(prefix);
		Map<String,String> map = new HashMap<String,String>();
		while(iterator.hasNext()){
			String key = iterator.next();
			String value = config.getString(key);
			int index = key.indexOf('.');
			key = key.substring(index+1);
			map.put(key, value);
		}
		return map;
	}

	/**
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getConfigString(String key, String defaultValue) {
		return config.getString(key, defaultValue);
	}

	public static int getConfigInt(String key) {
		return config.getInt(key);
	}

	public static int getConfigInt(String key, int defaultValue) {
		return config.getInt(key, defaultValue);
	}

	public static boolean getConfigBoolean(String key) {
		return config.getBoolean(key);
	}

	public static boolean getConfigBoolean(String key, boolean defaultValue) {
		return config.getBoolean(key, defaultValue);
	}

	public static double getConfigDouble(String key) {
		return config.getDouble(key);
	}

	public static double getConfigDouble(String key, double defaultValue) {
		return config.getDouble(key, defaultValue);
	}

	public static float getConfigFloat(String key) {
		return config.getFloat(key);
	}

	public static float getConfigFloat(String key, float defaultValue) {
		return config.getFloat(key, defaultValue);
	}

	public static long getConfigLong(String key) {
		return config.getLong(key);
	}

	public static long getConfigLong(String key, long defaultValue) {
		return config.getLong(key, defaultValue);
	}

	/**
	 * 使用逗号作为分隔符
	 * @param key
	 * @return
	 */
	public static String[] getConfigStringArray(String key) {
		return config.getStringArray(key);
	}
	
	public static void main(String[] args){
		load("E:\\javaws\\erp\\conf\\SystemConfig.xml");
		System.out.print(config.getString("respath"));
	}

}